﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Text.RegularExpressions;
public partial class admin1_Tour : System.Web.UI.Page
{
    eStoreDataContext db = new eStoreDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        rpt_Category.DataSource = db.Tours;
        rpt_Category.DataBind();
        if (!IsPostBack)
        {
            Load();
        }
    }
    protected void Load()
    {
        // ckfinder
        CKEditor_Description.Language = "vi";
        CKFinder.FileBrowser
        _FileBrowser = new CKFinder.FileBrowser();
        _FileBrowser.BasePath = "/ckfinder/";
        _FileBrowser.SetupCKEditor(CKEditor_Description);

        //fin
        CKEditor_DescriptionVN.Language = "vi";
        CKFinder.FileBrowser
        _FileBrowser1 = new CKFinder.FileBrowser();
        _FileBrowser1.BasePath = "/ckfinder/";
        _FileBrowser1.SetupCKEditor(CKEditor_DescriptionVN);

        ddl_Category.DataSource = db.Categories.Where(c => c.Activated == true);
        ddl_Category.DataValueField = "CategoryId";
        ddl_Category.DataTextField = "NameVN";
        ddl_Category.DataBind();

        ck_Location.DataSource = db.Locations.Where(p => p.CategoryId == int.Parse(ddl_Category.SelectedValue) && p.Activated == true);
        ck_Location.DataValueField = "LocationId";
        ck_Location.DataTextField = "NameVN";
        ck_Location.DataBind();

        ddl_Hotel.DataSource = db.Hotels;
        ddl_Hotel.DataValueField = "HotelId";
        ddl_Hotel.DataTextField = "Name";
        ddl_Hotel.DataBind();

        ddl_Transport.DataSource = db.Transports;
        ddl_Transport.DataValueField = "TransportId";
        ddl_Transport.DataTextField = "NameVN";
        ddl_Transport.DataBind();

    }
    protected void ddl_Category_SelectedIndexChanged(object sender, EventArgs e)
    {
        ck_Location.DataSource = db.Locations.Where(p => p.CategoryId == int.Parse(ddl_Category.SelectedValue) && p.Activated == true);
        ck_Location.DataValueField = "LocationId";
        ck_Location.DataTextField = "NameVN";
        ck_Location.DataBind();
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        try
        {
            var tour = new Tour
            {
                Name = txt_Name.Text,
                NameVN = txt_NameVN.Text,
                UnitPrice = double.Parse(txt_UnitPrice.Text),
                PriceChidren = int.Parse(txt_PriceChidren.Text),
                PriceBaby = int.Parse(txt_PriceBaby.Text),
                Image = txt_Image_Edit.Text,
                Description = CKEditor_Description.Text,
                DescriptionVN = CKEditor_DescriptionVN.Text,
                DescriptionPrice = CKEditor_Price.Text,
                DescriptionPriceVN = CKEditor_PriceVN.Text,
                TourDate = txt_SoNgay.Text,
                TourDateVN = txt_SoNgayVN.Text,
                Promotion = bool.Parse(rad_Proto.SelectedValue),
                Activated = bool.Parse(rad_Available.SelectedValue),
                HotelId = int.Parse(ddl_Hotel.SelectedValue),
                TransportId = int.Parse(ddl_Transport.SelectedValue),
                CategoryId = int.Parse(ddl_Category.SelectedValue),

            };

            //2.them vao
            db.Tours.InsertOnSubmit(tour);
            //3. luu
            db.SubmitChanges();
            int id = db.Tours.Max(p => p.TourId);
            for (int i = 0; i < ck_Location.Items.Count; i++)
            {

                if (ck_Location.Items[i].Selected == true)
                {
                    var content = new LocationTour
                    {
                        TourId = id,
                        LocationId = int.Parse(ck_Location.Items[i].Value)
                    };
                    db.LocationTours.InsertOnSubmit(content);
                }
            }
            db.SubmitChanges();
            // luu bang travel
            var travel = new Travel
           {
               TourId = id,
               DateStart = txt_DatStart.Text,
               DateStartVN=txt_DatStartVN.Text
           };
            db.Travels.InsertOnSubmit(travel);
            db.SubmitChanges();
            // luu bảng phụ thu
            var sur = new Surcharge
            {
                Foreigner = int.Parse(txt_Foreigner.Text),
                Singleroom = int.Parse(txt_Singleroom.Text),
                TourId = id
            };
            db.Surcharges.InsertOnSubmit(sur);
            db.SubmitChanges();
            Response.Redirect("Tour.aspx");
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    // xoa 
    protected void btnxoa_Click(object sender, EventArgs e)
    {
        int touid = int.Parse(((LinkButton)sender).CommandArgument.ToString());
        //Response.Write("<script>alert("+id+")</script>"); kiem tra id 
        //1. tim
        var tou = db.Tours.Single(c => c.TourId == touid);
        int dem = 0;
        foreach (var con in db.Orders)
        {
            if (con.TourId == touid)
            {
                if (dem > 0)
                    break;
                dem++;
            }
        }
        if (dem == 0)
        {
            //2. Xoa
            db.Tours.DeleteOnSubmit(tou);
            //3. luu
            db.SubmitChanges();
            Response.Redirect("Tour.aspx");
        }
        else
        {
            Response.Write("<script> alert('Xóa tour không thành công! Có khách đặt tour này!');</script>");
        }
    }

    protected void lbt_ClosAdd_Click(object sender, EventArgs e)
    {
        panel_Add.Visible = false;
    }
    protected void lbt_Add_New_Click(object sender, EventArgs e)
    {
        panel_Add.Visible = true;
    }

    #region - Create Thumb Image -

    public static Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight)
    {
        System.Drawing.Bitmap bmpOut = null;
        try
        {
            Bitmap loBMP = new Bitmap(lcFilename);
            ImageFormat loFormat = loBMP.RawFormat;

            decimal lnRatio;
            int lnNewWidth = 0;
            int lnNewHeight = 0;

            //*** If the image is smaller than a thumbnail just return it
            if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
                return loBMP;

            if (loBMP.Width > loBMP.Height)
            {
                lnRatio = (decimal)lnWidth / loBMP.Width;
                lnNewWidth = lnWidth;
                decimal lnTemp = loBMP.Height * lnRatio;
                lnNewHeight = (int)lnTemp;
            }
            else
            {
                lnRatio = (decimal)lnHeight / loBMP.Height;
                lnNewHeight = lnHeight;
                decimal lnTemp = loBMP.Width * lnRatio;
                lnNewWidth = (int)lnTemp;
            }
            bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
            Graphics g = Graphics.FromImage(bmpOut);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
            g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);

            loBMP.Dispose();
        }
        catch
        {
            return null;
        }

        return bmpOut;
    }

    #endregion

    //bool Regex.IsMach(String input, String Pattern);
    protected void txt_Name_TextChanged(object sender, EventArgs e)
    {

        foreach(var row in db.Tours)
        {
            if(row.Name==txt_Name.Text)
            {
                txt_NameVN.Text = "Tên tiếng anh đã tồn tại";
                break;
            }
        }
    }
}